<template>
  <div>
    <el-table
      border
      :data="tableData"
      style="width: 100%"
    >
      <el-table-column label="序号" width="120" type="index" />
      <el-table-column
        prop="name"
        label="角色"
        width="240"
      />
      <el-table-column
        prop="description"
        label="描述"
      />
      <el-table-column label="操作" prop="id">
        <template v-slot="{row}">
          <el-button size="mini" type="success" @click="addPer(row.id)">分配权限</el-button>
          <el-button size="mini" type="primary" @click="edit(row.id)">编辑</el-button>
          <el-button size="mini" type="danger" @click="del(row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <div class="block">
      <el-pagination
        layout="prev, pager, next"
        :total="Query.total"
        :page-size="Query.pagesize"
        :current-page="Query.page"
        @current-change="currentChange"
      />
    </div>

    <el-dialog title="分配权限" :visible.sync="showDialog" @close="cancel">
      <el-tree
        ref="permTree"
        :data="data"
        :props="{label:'name'}"
        default-expand-all
        show-checkbox
        check-strictly
        :default-checked-keys="dataChecked"
        node-key="id"
      />
      <el-row slot="footer" type="flex" justify="center">
        <el-col :span="6">
          <el-button size="samll" @click="cancel">取消</el-button>
          <el-button size="samll" type="primary" @click="assignPerm">确认</el-button>
        </el-col>
      </el-row>
    </el-dialog>
  </div>

</template>

<script>
import { getPeopleList, delPeople, getPeople, assignPerm } from '@/api/setting.js'
import { getPermissionList } from '@/api/permission'
import { transListToTreeData } from '@/utils'
export default {
  data() {
    return {
      tableData: [],
      Query: {
        page: 1, // 当前页马数
        pagesize: 5, // 每页显示条数
        total: 0
      },
      showDialog: false, // 分配权限弹出层
      data: [], // 树形数据权限列表
      dataChecked: [], // 已经拥有的权限
      roleId: null // 记录被选中的角色

    }
  },
  created() {
    this.getPeopleList()
  },
  methods: {
    // 获取角色列表的方法
    async getPeopleList() {
      const res = await getPeopleList(this.Query)
      // console.log(res)// {total:1,rows:[]}
      this.Query.total = res.total
      this.tableData = res.rows
    },
    // 切换页码
    currentChange(newpage) {
      this.Query.page = newpage
      this.getPeopleList()
    },
    // 删除角色
    del(id) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        delPeople(id)// 根据id删除角色
        if (this.Query.page > 1 && this.tableData.length === 1) { // 这里进行删除优化,防止当前页没数据显示问题
          this.Query.page--
        }
        this.getPeopleList()// 刷新角色列表
        this.$message({
          type: 'success',
          message: '删除成功!'
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    // 编辑角色
    edit(id) {
      this.$emit('editPeople', id)
    },
    // 打开分配权限弹窗
    async addPer(id) {
      this.data = transListToTreeData(await getPermissionList({ page: 1, pagesize: 100 }), '0')
      this.roleId = id
      const { permIds } = await getPeople(id)
      this.dataChecked = permIds
      this.showDialog = true
    },
    // 取消
    cancel() {
      this.dataChecked = []
      this.showDialog = false
    },
    // 分配权限
    async assignPerm() {
      try {
        // getCheckedKeys() 是el-tree的方法,获取所有选中的节点的key --因为this.dataChecked不能实时更新
        await assignPerm({ id: this.roleId, permIds: this.$refs.permTree.getCheckedKeys() })
        this.$message.success('分配权限成功!')
        this.showDialog = false
      } catch (error) {
        console.log(error)
      }
    }
  }
}
</script>

<style scoped>
.block{
  display: flex;
  justify-content: right;
  margin-top: 10px;
}
</style>
